﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.ExtensibleStorage;
using YArchitech.Revit;

namespace YArchitech.MEP
{
	public class ClearProtectRange
	{
		public ClearProtectRange()
		{
		}

		public ClearProtectRange(Autodesk.Revit.DB.Document doc, List<ElementFilter> elementFilterList, HyRangeCheckMajor rangeCheckApplicatScenarios)
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
			ICollection<Element> collection = null;
			if (rangeCheckApplicatScenarios == HyRangeCheckMajor.HyGPS)
			{
				if (elementFilterList.Count == 1)
				{
					ElementCategoryFilter elementCategoryFilter = elementFilterList.ElementAt(0) as ElementCategoryFilter;
					collection = filteredElementCollector.WherePasses(elementCategoryFilter).WhereElementIsNotElementType().ToElements();
				}
				else if (elementFilterList.Count == 2)
				{
					ElementFilter elementFilter = elementFilterList.ElementAt(0) as ElementCategoryFilter;
					ElementCategoryFilter elementCategoryFilter2 = elementFilterList.ElementAt(1) as ElementCategoryFilter;
					LogicalOrFilter logicalOrFilter = new LogicalOrFilter(elementFilter, elementCategoryFilter2);
					collection = filteredElementCollector.WherePasses(logicalOrFilter).WhereElementIsNotElementType().ToElements();
				}
				else
				{
					LogicalOrFilter logicalOrFilter2 = new LogicalOrFilter(elementFilterList);
					collection = filteredElementCollector.WherePasses(logicalOrFilter2).WhereElementIsNotElementType().ToElements();
				}
			}
			else if (rangeCheckApplicatScenarios == HyRangeCheckMajor.HyDQ)
			{
				ElementCategoryFilter elementCategoryFilter3 = elementFilterList.ElementAt(0) as ElementCategoryFilter;
				collection = filteredElementCollector.WherePasses(elementCategoryFilter3).WhereElementIsNotElementType().ToElements();
			}
			foreach (Element element in collection)
			{
				this.DeleteTags(doc, element);
			}
		}

		public void DeletOldDetailCurve(Autodesk.Revit.DB.Document doc, Element elem)
		{
			Parameter parameterByName = this.GetParameterByName(elem, "Hy_" + elem.Category.Name + "保护范围");
			if (parameterByName.HasValue)
			{
				char[] separator = new char[]
				{
					','
				};
				foreach (string value in parameterByName.AsString().Split(separator, StringSplitOptions.RemoveEmptyEntries))
				{
					foreach (ElementId elementId in new FilteredElementCollector(doc).OfClass(typeof(CurveElement)).ToElementIds())
					{
						if (elementId.ToString().Equals(value))
						{
							using (SubTransaction subTransaction = new SubTransaction(doc))
							{
								subTransaction.Start();
								doc.Delete(elementId);
								subTransaction.Commit();
								break;
							}
						}
					}
				}
				this.GetParameterByName(elem, "Hy_" + elem.Category.Name + "保护范围").Set("");
			}
		}

		private Parameter GetParameterByName(Element elem, string strParamName)
		{
			Parameter result = null;
			IList<Parameter> parameters = elem.GetParameters(strParamName);
			if (parameters.Count != 0)
			{
				result = parameters.ElementAt(0);
			}
			return result;
		}

		public void DeleteTags(Autodesk.Revit.DB.Document doc, Element element)
		{
			Guid guid = new Guid("C0C7C53C-8A31-4562-88A0-293290913CA8");
			if (Schema.Lookup(guid) == null)
			{
				return;
			}
			Entity entity = element.Document.IsLinked ? doc.ActiveView.GetEntity(Schema.Lookup(guid)) : element.GetEntity(Schema.Lookup(guid));
			if (entity.Schema == null)
			{
				return;
			}
			string text = entity.Get<string>(Schema.Lookup(guid).GetField("RecordTagElems"));
			if (string.IsNullOrEmpty(text))
			{
				return;
			}
			char[] separator = new char[]
			{
				'@'
			};
			string[] array = text.Split(separator, StringSplitOptions.RemoveEmptyEntries);
			for (int i = 0; i < array.Length; i++)
			{
				int num;
				if (int.TryParse(array[i], out num))
				{
					ElementId elementId = new ElementId(num);
					Element element2 = doc.GetElement(elementId);
					if (element2 != null)
					{
						doc.DeleteElement(element2);
					}
				}
			}
		}
	}
}
